﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using bookmeetingroom.Data.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;


namespace bookmeetingroom.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {

            optionsBuilder.EnableSensitiveDataLogging();
        }
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {

        }


        public DbSet<Booking> Bookings { get; set; }

        public DbSet<BookingResult> BookingResults { get; set; }

        public DbSet<Company> Companies { get; set; }

        public DbSet<Facility> Facilities { get; set; }


        public DbSet<MeetingRoom> MeetingRooms { get; set; }

        public DbSet<Code> Codes { get; set; }

        public DbSet<Building> Buildings { get; set; }

        public DbSet<Feedback> Feedbacks { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<Code>().ToTable("T_Code");

            var bookingtbl = builder.Entity<Booking>().ToTable("T_Booking");
            bookingtbl.HasIndex(b => b.UserId);
            bookingtbl.HasIndex(b => b.MeetingRoomId);
            bookingtbl.HasIndex(b => b.CompanyId);
            bookingtbl.HasOne(n => n.MeetingRoom).WithMany(n => n.Bookings).HasForeignKey(n => n.MeetingRoomId).OnDelete(DeleteBehavior.Cascade);



            var bookingresulttbl = builder.Entity<BookingResult>().ToTable("T_BookingResult");
            bookingresulttbl.HasIndex(b => b.BookingId);



            var meetingroomtbl = builder.Entity<MeetingRoom>().ToTable("T_MeetingRoom");
            meetingroomtbl.HasIndex(b => b.BuildingId);
            meetingroomtbl.HasIndex(b => b.CompanyId);
            meetingroomtbl.HasMany(n => n.MeetingRoomFacilities).WithOne().OnDelete(DeleteBehavior.Cascade);


            var meetingRoomFacilitytbl = builder.Entity<MeetingRoomFacility>().ToTable("T_MeetingRoomFacility");
            meetingRoomFacilitytbl.HasKey(bc => new { bc.MeetingRoomId, bc.FacilityId });
            meetingRoomFacilitytbl.HasOne(b => b.Facility).WithMany(b => b.MeetingRoomFacilities).HasForeignKey(b => b.FacilityId).OnDelete(DeleteBehavior.Cascade);
            meetingRoomFacilitytbl.HasOne(b => b.MeetingRoom).WithMany(b => b.MeetingRoomFacilities).HasForeignKey(b => b.MeetingRoomId).OnDelete(DeleteBehavior.Cascade);

            // meetingroomtbl.HasMany(n => n.Facilities).WithMany(n => n.MeetingRooms)
            //.Map(m =>
            //   {
            //       m.ToTable("MeetingRoomFacility");
            //       m.MapLeftKey("MeetingRoomId");
            //       m.MapRightKey("FacilityId");
            //   });

            var buildingtbl = builder.Entity<Building>().ToTable("T_Building");
            buildingtbl.HasIndex(b => b.CompanyId);

            var companytbl = builder.Entity<Company>().ToTable("T_Company");

            var facilitytbl = builder.Entity<Facility>().ToTable("T_Facility");

            builder.Entity<Feedback>().ToTable("T_Feedback").HasIndex(b => b.UserId); ; ;

        }
    }
}